package com.app.reportproblem;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.model.GraphUser;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class LoginFBActivity extends Activity {
	
	 // Your Facebook APP ID
    private static String APP_ID = "496033847186087"; // Replace your App ID here
 
    // Instance of Facebook Class
    private Facebook facebook;
    private AsyncFacebookRunner mAsyncRunner;
    String FILENAME = "AndroidSSO_data";
    private SharedPreferences mPrefs;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_fb);
		
		facebook = new Facebook(APP_ID);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
        
        loginToFacebook();
	}
	
	
	public void loginToFacebook() {
		// start Facebook Login
	    Session.openActiveSession(this, true, new Session.StatusCallback() {

	        // callback when session changes state
	        @Override
	        public void call(Session session, SessionState state,
	                Exception exception) {
	            if (session.isOpened()) {

	                // make request to the /me API
	                Request.executeMeRequestAsync(session,
	                        new Request.GraphUserCallback() {

	                            // callback after Graph API response with user
	                            // object
	                            @Override
	                            public void onCompleted(GraphUser user,
	                                    Response response) {
	                                if (user != null) {
	                                	Log.i("DATA", "NAME: "+user.getName());
	                                }
	                            }

	                        });
	            }
	        }

	    });
		
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode,
	            resultCode, data);
	}


	@Override
	protected void onResume() {
		super.onResume();
		 Session session = Session.getActiveSession();
		    if (session.isOpened()) {
		        Toast.makeText(LoginFBActivity.this, session.getAccessToken(), Toast.LENGTH_LONG).show();
		    }
	}
	
	

}
